package com.qfedu.food.util;

import com.qfedu.food.config.SystemConfig;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;
public class RedissonUtil {
    //实例化
    public static RedissonClient client;
    static {
        Config config=new Config();
        config.useSingleServer().
                setAddress("redis://"+ SystemConfig.REDIS_HOST+":"+SystemConfig.REDIS_PORT).
                setPassword(SystemConfig.REDIS_PASS);
//        config.useSingleServer();//单机
//        config.useSentinelServers();//哨兵模式
//        config.useClusterServers();//集群模式
        client= Redisson.create(config);
    }
    public static void delKeys(String... keys){
        client.getKeys().delete(keys);
    }
    public static void delField(String key,String field){
        client.getMap(key).remove(field);
    }
    //查询
    public static String getStr(String key){
        return client.getBucket(key).get().toString();
    }
    public static String getHash(String key,String field){
        return client.getMap(key).get(field).toString();
    }
    public static boolean checkField(String key,String field){
        return client.getMap(key).containsKey(field);
    }
    public static boolean isHave(String key){
        return client.getKeys().countExists(key)>0;
    }
    public static void expire(String key,int seconds){
        client.getKeys().expire(key,seconds, TimeUnit.SECONDS);
    }
}
